package com.itboye.shop;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.shop.app.MyApplication;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.logincontroller.LoginController;
import com.itboye.shop.logincontroller.LoginedState;
import com.itboye.shop.logincontroller.UnLoginState;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.SPUtils;

public class MainActivity extends BaseActivity {


    View me_layout, index_layout, message_layout, store_layout;
    private FragmentManager fragmentManager;
    private IndexFragment indexFragment;
    private StoreFragment storeFragment;
    private FragmentShopcart messageFragment;
    private MeFragment meFragment;
//    UserPresenter userPresenter;
    public static MainActivity act;

    public static MainActivity onSetFragment() {
        return act;

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        act = this;
        fragmentManager = getSupportFragmentManager();
        // 第一次启动时选中第0个tab
        setTabSelection(0);
    }

    private void initView() {
        message_layout = findViewById(R.id.message_layout);
        store_layout = findViewById(R.id.store_layout);
        index_layout = findViewById(R.id.index_layout);
        me_layout = findViewById(R.id.me_layout);
        message_layout.setOnClickListener(this);
        index_layout.setOnClickListener(this);
        store_layout.setOnClickListener(this);
        me_layout.setOnClickListener(this);

        /**
         * 登录状态
         */
        if ((Boolean) SPUtils.get(MyApplication.instatnce, null,
                Const.IS_LOGINED, false)) {
            LoginController.setLoginState(new LoginedState());
            Log.v("logined", "已经登录");
        } else {
            LoginController.setLoginState(new UnLoginState());
            Log.v("logined", "未登录");
        }
    }


    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     * @param index 每个tab页对应的下标。0表示消息，1表示联系人，2表示动态，3表示设置。
     */
    @SuppressLint("ResourceAsColor")
    public void setTabSelection(int index) {
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection();
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0:
                // 当点击了首页tab时，改变控件的图片和文字颜色
                setStatusBarColor(R.color.white);

                ((ImageView) findViewById(R.id.index_image)).setImageResource(R.drawable.shouye_selected);
                ((TextView) findViewById(R.id.index_text))
                        .setTextColor(getResources().getColor(R.color.blue));
                if (indexFragment == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    indexFragment = new IndexFragment();
                    transaction.add(R.id.content, indexFragment);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(indexFragment);
                }
                break;
            case 1:
                setStatusBarColor(R.color.white);

                // 当点击了消息tab时，改变控件的图片和文字颜色
                ((ImageView) findViewById(R.id.store_image)).setImageResource(R.drawable.shangcheng_selected);
                ((TextView) findViewById(R.id.store_text)).setTextColor(getResources().getColor(R.color.blue));
                if (storeFragment == null) {
                    // 如果ContactsFragment为空，则创建一个并添加到界面上
                    storeFragment = new StoreFragment();
                    transaction.add(R.id.content, storeFragment);
                } else {
                    // 如果ContactsFragment不为空，则直接将它显示出来
                    transaction.show(storeFragment);
                }
                break;
            case 2:
                setStatusBarColor(R.color.white);

                // 当点击了我的tab时，改变控件的图片和文字颜色
                ((ImageView) findViewById(R.id.message_image)).setImageResource(R.drawable.xiaoxi_selected);
                ((TextView) findViewById(R.id.message_text)).setTextColor(getResources().getColor(R.color.blue));

                if (messageFragment == null) {
                    // 如果SettingFragment为空，则创建一个并添加到界面上
                    messageFragment = new FragmentShopcart();
                    transaction.add(R.id.content, messageFragment);
                } else {
                    // 如果SettingFragment不为空，则直接将它显示出来
                    transaction.show(messageFragment);
                }
                break;
            case 3:
                setStatusBarColor(R.color.blue);
                // 当点击了服务tab时，改变控件的图片和文字颜色
                ((ImageView) findViewById(R.id.me_image)).setImageResource(R.drawable.geren_selected);
                ((TextView) findViewById(R.id.me_text)).setTextColor(getResources().getColor(R.color.blue));
                if (meFragment == null) {
                    // 如果NewsFragment为空，则创建一个并添加到界面上
                    meFragment = new MeFragment();
                    transaction.add(R.id.content, meFragment);
                } else {
                    // 如果NewsFragment不为空，则直接将它显示出来
                    transaction.show(meFragment);
                }

                break;

            default:

                break;
        }
        transaction.commit();
    }

    /**
     * 清除掉所有的选中状态。
     */

    private void clearSelection() {
        ((ImageView) findViewById(R.id.message_image)).setImageResource(R.drawable.xiaoxi01);
        ((TextView) findViewById(R.id.message_text)).setTextColor(getResources().getColor(R.color.home_bottom_gray));
        ((ImageView) findViewById(R.id.index_image)).setImageResource(R.drawable.shouye);
        ((TextView) findViewById(R.id.index_text)).setTextColor(getResources().getColor(R.color.home_bottom_gray));
        ((ImageView) findViewById(R.id.store_image)).setImageResource(R.drawable.shangcheng);
        ((TextView) findViewById(R.id.store_text)).setTextColor(getResources().getColor(R.color.home_bottom_gray));
        ((ImageView) findViewById(R.id.me_image)).setImageResource(R.drawable.geren);
        ((TextView) findViewById(R.id.me_text)).setTextColor(getResources().getColor(R.color.home_bottom_gray));

    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (indexFragment != null) {
            transaction.hide(indexFragment);
        }
        if (storeFragment != null) {
            transaction.hide(storeFragment);
        }
        if (messageFragment != null) {
            transaction.hide(messageFragment);
        }
        if (meFragment != null) {
            transaction.hide(meFragment);
        }

    }
//    https://git.oschina.net/gooraye/itboye_shop_android
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.me_layout:
                // 当点击了消息tab时，选中第1个tab
                setTabSelection(3);
                break;
            case R.id.index_layout:
                setTabSelection(0);
                break;
            case R.id.message_layout:
                setTabSelection(2);
                break;
            case R.id.store_layout:
                setTabSelection(1);
                break;
        }
    }
}
